package com.swufestu.hello;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateList2Activity extends ListActivity implements Runnable, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private String TAG="mylist2";
    Handler handler;
    private ArrayList<HashMap<String,String>> listItems;//存放文字、图片信息
    private SimpleAdapter listItemAdatper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_rate_list2);
        initListView();
        //MyAdapter myAdapter = new MyAdapter(this,R.layout.list_item,listItems);
        this.setListAdapter(listItemAdatper);
        Thread t = new Thread(this);
        t.start();//启动
        handler = new Handler(Looper.myLooper()) {//获得当前线程的对象
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 7) {
                    listItems= (ArrayList<HashMap<String, String>>) msg.obj;//list包要导入正确
                    listItemAdatper = new SimpleAdapter(RateList2Activity.this,listItems,
                            R.layout.list_item,//xml布局实现
                            new String[]{"ItemTitle","ItemDetail"},
                            new int[]{R.id.itemTitle,R.id.itemDetail }
                    );
                    setListAdapter(listItemAdatper);
                }
                super.handleMessage(msg);
            }
        };
        getListView().setOnItemClickListener(this);
        getListView().setOnItemLongClickListener(this);
    }
    private void initListView(){
        listItems = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<10;i++){
            HashMap<String,String> map=new HashMap<String,String>();
            map.put("ItemTitle","rate: "+i);//标题文字
            map.put("ItemDetail","detail"+i);//详情描述
            listItems.add(map);
        }
        //生成适配器 的item和动态数组对应的元素
        listItemAdatper = new SimpleAdapter(this,listItems,
                R.layout.list_item,//xml布局实现
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.itemTitle,R.id.itemDetail }
        );
    }

    @Override
    public void run() {
        List<HashMap<String,String>> retList = new ArrayList<HashMap<String,String>>();
        Document doc=null;
        try{
            Thread.sleep(3000);
            doc = Jsoup.connect("https://usd-cny.com/").get();
            Elements tables = doc.getElementsByTag("table");
            Element firstTable = tables.first();//从集合中直接获得，避免标签不同产生问题
            Elements trs = firstTable.getElementsByTag("tr");
            trs.remove(0);//或者直接去除第一行
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                Element td1 = tds.get(0);
                Element td2 = tds.get(4);
                Log.i(TAG, "run: "+td1.text()+"——>"+td2.text());
                String str1=td1.text();
                String str2=td2.text();
                HashMap<String,String> map=new HashMap<String,String>();
                map.put("ItemTitle",str1);
                map.put("ItemDetail",str2);
                retList.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Message msg= handler.obtainMessage(7);
        msg.obj = retList;
        handler.sendMessage(msg);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick: parent="+parent);
        Log.i(TAG, "onItemClick: view="+view);
        Log.i(TAG, "onItemClick: position="+position);
        Log.i(TAG, "onItemClick: id="+id);
        HashMap<String,String> map= (HashMap<String, String>) getListView().getItemAtPosition(position);
        String titlestr=map.get("ItemTitle");
        String detailStr=map.get("ItemDetail");
        TextView title1 = view.findViewById(R.id.itemTitle);
        TextView detail = view.findViewById(R.id.itemDetail);
        String title2= String.valueOf(title1.getText());
        String detail2 = String.valueOf(detail.getText());
        Intent rateCalc = new Intent(this,CalcActivity.class);
        rateCalc.putExtra("title",titlestr);
        rateCalc.putExtra("rate",Float.parseFloat(detailStr));
        startActivity(rateCalc);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: 长按操作");
        AlertDialog.Builder bulider = new AlertDialog.Builder(this);
        bulider.setTitle("提示")
                .setMessage("确认是否删除？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick: 对话框事件处理");
                        listItems.remove(position);
                        Log.i(TAG, "onClick: position"+position);
                        listItemAdatper.notifyDataSetChanged();
                    }
                }).setNegativeButton("否",null);
        bulider.create().show();
        return true;
        //变为true：长按操作返回后会与单击区分开
    }
}



